home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C03 / OnTheFly.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.1 KB  |  39 lines

  1. //: C03:OnTheFly.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // On-the-fly variable definitions
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. int main() {
  11.   //..
  12.   { // Begin a new scope
  13.     int q = 0; // C requires definitions here
  14.     //..
  15.     // Define at point of use:
  16.     for(int i = 0; i < 100; i++) { 
  17.       q++; // q comes from a larger scope
  18.       // Definition at the end of the scope:
  19.       int p = 12; 
  20.     }
  21.     int p = 1;  // A different p
  22.   } // End scope containing q & outer p
  23.   cout << "Type characters:" << endl;
  24.   while(char c = cin.get() != 'q') {
  25.     cout << c << " wasn't it" << endl;
  26.     if(char x = c == 'a' || c == 'b')
  27.       cout << "You typed a or b" << endl;
  28.     else
  29.       cout << "You typed " << x << endl;
  30.   }
  31.   cout << "Type A, B, or C" << endl;
  32.   switch(int i = cin.get()) {
  33.     case 'A': cout << "Snap" << endl; break;
  34.     case 'B': cout << "Crackle" << endl; break;
  35.     case 'C': cout << "Pop" << endl; break;
  36.     default: cout << "Not A, B or C!" << endl;
  37.   }
  38. } ///:~
  39.